import time
from selenium.webdriver.common.by import By
from common.Utils import BlogDriver


class BlogDetail:
    def __init__(self):
        # 先访问博客列表获取第一篇博客的ID
        self.driver = BlogDriver.driver
        self.driver.get("http://8.137.19.140:9090/blog_list.html")
        time.sleep(1)  # 等待页面加载

        try:
            # 获取第一篇博客的链接
            first_blog_link = self.driver.find_element(
                By.CSS_SELECTOR,
                "body > div.container > div.right > div:nth-child(1) > a"
            )
            blog_url = first_blog_link.get_attribute("href")
            print(f"获取到博客链接: {blog_url}")

            # 访问博客详情页
            self.driver.get(blog_url)
            time.sleep(1)  # 等待页面加载
        except Exception as e:
            print("获取博客详情失败:", str(e))
            BlogDriver.getScreeShot()

    def DetailTestByLogin(self):
        """登录状态下的详情页测试"""
        try:
            # 检查标题
            title = self.driver.find_element(
                By.CSS_SELECTOR,
                "body > div.container > div.right > div > h3"
            )
            print(f"博客标题: {title.text}")

            # 检查时间
            date = self.driver.find_element(
                By.CSS_SELECTOR,
                "body > div.container > div.right > div > div.date"
            )
            print(f"发布日期: {date.text}")

            # 检查内容
            content = self.driver.find_element(
                By.CSS_SELECTOR,
                "#content"
            )
            print(f"博客内容: {content.text[:50]}...")

            BlogDriver.getScreeShot()
            return True
        except Exception as e:
            print("详情页测试失败:", str(e))
            BlogDriver.getScreeShot()
            return False